package com.gomyb.redis.serializer;

import com.gomyb.redis.utils.KryoUtil;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/9/2 17:44
 */
public class KryoRedisSerializer<T> implements RedisSerializer<T> {

    private Class<T> type;

    public KryoRedisSerializer(Class<T> type) {
        this.type = type;
    }

    @Override
    public byte[] serialize(T t) throws SerializationException {
        return KryoUtil.writeObjectToByteArray(t);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        return bytes != null ? KryoUtil.readObjectFromByteArray(bytes, type) : null;
    }
}
